package com.paireach.szwolaiyun.mode;

import android.text.TextUtils;
import android.util.Log;

import com.paireach.szwolaiyun.util.StringUtil;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 订单信息mode
 * Created by Administrator on 2016/6/22.
 */
public class DispatchMode {

    private boolean isHaveDispatch;//是否有运单
    private String code;//单号

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    private String  id;//运单id
    private String date;//装运日期
    private String deliveryPlant;//发货工厂
    private String deliveryPlantAddress;//发货工程地址
    private String receivingPlant;//收货工厂
    private String receivingPlantAddress;//收货工厂地址
    private String statusCode=StringUtil.HAS_BEEN_COMPLETED;//状态默认为无运单
    private String statusName;
    private String planDeliverStartTime;//预计装货开始时间
    private String planDeliverEndTime;//预计装货结束时间
    private String sourceLat;//发货工厂经纬度
    private String sourceLng;//发货工厂经纬度
    private String warehouseType;//内仓，外仓类型 ，  10内仓，11外仓
    private String scSupportBking;//是否排队，1排队，2不排队

    public String getScSupportBking() {
        return scSupportBking;
    }

    public void setScSupportBking(String scSupportBking) {
        this.scSupportBking = scSupportBking;
    }

    public String getWarehouseType() {
        return warehouseType;
    }

    public void setWarehouseType(String warehouseType) {
        this.warehouseType = warehouseType;
    }

    public String getSourceLat() {
        return sourceLat;
    }

    public void setSourceLat(String sourceLat) {
        this.sourceLat = sourceLat;
    }

    public String getSourceLng() {
        return sourceLng;
    }

    public void setSourceLng(String sourceLng) {
        this.sourceLng = sourceLng;
    }

    public String getPlanDeliverStartTime() {
        return planDeliverStartTime;
    }

    public void setPlanDeliverStartTime(String planDeliverStartTime) {
        this.planDeliverStartTime = planDeliverStartTime;
    }

    public String getPlanDeliverEndTime() {
        return planDeliverEndTime;
    }

    public void setPlanDeliverEndTime(String planDeliverEndTime) {
        this.planDeliverEndTime = planDeliverEndTime;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusName() {
        return statusName;
    }

    public void setStatusName(String statusName) {
        this.statusName = statusName;
    }

    public boolean isHaveDispatch() {
        return isHaveDispatch;
    }

    public void setIsHaveDispatch(boolean isHaveDispatch) {
        this.isHaveDispatch = isHaveDispatch;
    }
    public  boolean getIsHaveDispatch(){
        return isHaveDispatch;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDeliveryPlant() {
        return deliveryPlant;
    }

    public void setDeliveryPlant(String deliveryPlant) {
        this.deliveryPlant = deliveryPlant;
    }

    public String getDeliveryPlantAddress() {
        return deliveryPlantAddress;
    }

    public void setDeliveryPlantAddress(String deliveryPlantAddress) {
        this.deliveryPlantAddress = deliveryPlantAddress;
    }

    public String getReceivingPlant() {
        return receivingPlant;
    }

    public void setReceivingPlant(String receivingPlant) {
        this.receivingPlant = receivingPlant;
    }

    public String getReceivingPlantAddress() {
        return receivingPlantAddress;
    }

    public void setReceivingPlantAddress(String receivingPlantAddress) {
        this.receivingPlantAddress = receivingPlantAddress;
    }

    public void init(String jsonString){
        try {
            JSONObject jsonObject=new JSONObject(jsonString);
            setIsHaveDispatch(jsonObject.optBoolean("resultFlag"));
            String jsonArrayStr=jsonObject.optString("responseEntity");
            if(TextUtils.isEmpty(jsonArrayStr)||jsonArrayStr.equals("null")){
                jsonArrayStr="";
            }
            JSONArray jsonArray=new JSONArray(jsonArrayStr);
            if(null!=jsonArray&&jsonArray.length()!=0){
                JSONObject dispatchObject=jsonArray.getJSONObject(0);
                setCode(dispatchObject.optString("code"));
                Log.e("0011", "初始化运单号为" + dispatchObject.optString("code"));
                setDate(dispatchObject.optString("planDeliverTime").replace("00:00:00", ""));
                setDeliveryPlant(dispatchObject.optString("sourceName"));
                setDeliveryPlantAddress(dispatchObject.optString("sourceAddress"));
                setReceivingPlant(dispatchObject.optString("dcName"));
                setReceivingPlantAddress(dispatchObject.optString("dcAddress"));
                setStatusCode(dispatchObject.optString("status"));
                setStatusName(dispatchObject.optString("statusName"));
                setSourceLat(dispatchObject.optString("sourceLat"));
                setSourceLng(dispatchObject.optString("sourceLng"));
                setPlanDeliverStartTime(dispatchObject.optString("planDeliverStartTime"));
                setPlanDeliverEndTime(dispatchObject.optString("planDeliverEndTime"));
                setScSupportBking(dispatchObject.optString("scSupportBking"));
                setWarehouseType(dispatchObject.optString("warehouseType"));
                setId(dispatchObject.optString("id"));
                //如果是已签收或者是已完成，设置运单状态为无运单
                if(dispatchObject.optString("status").equals(StringUtil.ALREADY_SIGN)||dispatchObject.optString("status").equals(StringUtil.HAS_BEEN_COMPLETED)){
                    setIsHaveDispatch(false);
                }

            }else{
                //如果responseEntity字段对应值为null,那么设置为无运单状态
                setIsHaveDispatch(false);
            }

        } catch (Exception e) {
            e.printStackTrace();
            setIsHaveDispatch(false);
            Log.e("0011","解析JSON出现错误");
        }
    }
}
